package com.changan.exception;

import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;


/**
 * @program: t131smbms
 * @description: 全局异常处理,表示这个类能处理控制器的异常
 * @author: Mr.Nie
 * @create: 2021-12-01 13:48
 **/
@ControllerAdvice
public class GlobalException {

    /**
     * 功能描述: <br>
     * 〈专门用来处理BindException〉
     * @Param: [ex]
     * @Return: java.lang.String
     * @Author: Mr.Nie
     * @Date: 2021/12/1 13:51
     */
    @ExceptionHandler(value = BindException.class)
    public String bindException(BindException ex, Model model){
        System.out.println(ex.getAllErrors().get(0).getDefaultMessage());
        model.addAttribute("error",ex.getAllErrors().get(0).getDefaultMessage());
        return "login";
    }

    @ExceptionHandler(value = ArithmeticException.class)
    public String arithmeticException(ArithmeticException ex, Model model){
        model.addAttribute("error","算数异常");
        return "login";
    }

    @ExceptionHandler(value = Exception.class)
    public String exception(Exception ex, Model model){
        model.addAttribute("error","未知的异常,请稍后重试...");
        ex.printStackTrace();
        return "login";
    }
}
